package org.agent.aigccore.dao.impl;

import jakarta.annotation.Resource;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.agent.aigccore.basic.enums.IotWorkOrderStatusEnum;
import org.agent.aigccore.dao.IotWorkOrderRepository;
import org.agent.aigccore.dao.mappers.mysql.IotWorkOrderMapper;
import org.agent.aigccore.model.bo.IotWorkOrderBO;
import org.agent.aigccore.model.po.IotWorkOrderPO;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

import static org.agent.aigccore.model.bo.IotWorkOrderBO.convert;

@Slf4j
@Setter
@Component
public class IotWorkOrderRepositoryImpl implements IotWorkOrderRepository {

    @Resource
    private IotWorkOrderMapper iotWorkOrderMapper;

    @Override
    public IotWorkOrderBO load(Long id, boolean lock) {
        if (lock) {
            return convert.formPO(iotWorkOrderMapper.lock(id));
        }
        return convert.formPO(iotWorkOrderMapper.load(id));
    }

    @Override
    public List<IotWorkOrderBO> page(IotWorkOrderStatusEnum status, Long businessGroupId, int offset, int pageSize) {
        List<IotWorkOrderPO> pos = iotWorkOrderMapper.page(
                status != null ? status.getCode() : null,
                businessGroupId,
                offset, pageSize);
        log.info("[IotWorkOrderRepository.page] 召回数量={}", pos.size());
        return pos.stream().map(po -> convert.formPO(po)).collect(Collectors.toList());
    }

    @Override
    public List<IotWorkOrderBO> queryByName(String name) {
        List<IotWorkOrderPO> pos = iotWorkOrderMapper.queryByName(name);
        log.info("[IotWorkOrderRepository.queryByName] 召回数量={}", pos.size());
        return pos.stream().map(po -> convert.formPO(po)).collect(Collectors.toList());
    }

    @Override
    public List<IotWorkOrderBO> queryByOutBizId(String outBizId) {
        List<IotWorkOrderPO> pos = iotWorkOrderMapper.queryByOutBizId(outBizId);
        log.info("[IotWorkOrderRepository.queryByOutBizId] 召回数量={}", pos.size());
        return pos.stream().map(po -> convert.formPO(po)).collect(Collectors.toList());
    }

    @Override
    public int insert(IotWorkOrderBO iotWorkOrderBO) {
        IotWorkOrderPO po = convert.toPO(iotWorkOrderBO);
        int num = iotWorkOrderMapper.insert(po);
        iotWorkOrderBO.setId(po.getId());
        log.info("[IotWorkOrderRepository.insert] 插入成功！num={}", num);
        return num;
    }

    @Override
    public int update(IotWorkOrderBO iotWorkOrderBO) {
        IotWorkOrderPO po = convert.toPO(iotWorkOrderBO);
        int num = iotWorkOrderMapper.update(po);
        log.info("[IotWorkOrderRepository.update] 更新成功！num={}", num);
        return num;
    }

    @Override
    public int delete(Long id) {
        int num = iotWorkOrderMapper.delete(id);
        log.info("[IotWorkOrderRepository.delete] 删除成功！num={}", num);
        return num;
    }
}